package kr.hkit.summary_java.serviceui;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;

public class PanelListUIEmp extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTable table;
	private DaoInterface<Employee> dao;
	private ArrayList<Employee> lists;
	private String[][] arLists;

	public PanelListUIEmp() {
		initialize();
	}
	
	public void setDao(DaoInterface<Employee> dao) {
		this.dao = dao;
		loadData();
		tableCellAlginAndWidth();
	}


	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		table = new JTable();
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
	}
	
	public void loadData(){
		try {
			lists = dao.listItems();
			arLists = new String[lists.size()][3];
			for(int i=0; i<lists.size();i++){
				arLists[i]=lists.get(i).toArray();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			String[] colNames={"사원번호", "이름", "부서번호", "직책","사수", "급여"};
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}
	
	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table.getColumnModel();
		
		cModel.getColumn(0).setPreferredWidth(70);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(2).setPreferredWidth(70);
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(3).setPreferredWidth(70);
		cModel.getColumn(3).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(4).setPreferredWidth(70);
		cModel.getColumn(4).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(5).setPreferredWidth(120);
		cModel.getColumn(5).setCellRenderer(dtcrRight);
	}


}
